package com.hyl.controller;

import com.hyl.entity.SysUser;
import com.hyl.service.UserService;
import com.hyl.uitl.FileUtil;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Description TODO
 * @Author com.hyl
 * @Date 2022/10/15 9:32
 **/
@Controller
@RequestMapping("/user")
public class UserController {

	Logger logger = Logger.getLogger(UserController.class);


	@Resource
	UserService userService;

	@RequestMapping("/list")
	public String list(String queryRealName, String queryRoleId,
	                   @RequestParam(defaultValue = "1") Integer pageIndex,
	                   Model model){
		logger.info("列表");
		int offset = (pageIndex-1) * 5;
		List<SysUser> list = userService.findByParam(queryRealName,queryRoleId,0);
		model.addAttribute("userList", list);
		return "sysUser/list";
	}


	@RequestMapping("/toAdd")
	public String toAdd(){
		logger.info("进入添加页面");
		int tmp = 1/0;
		return "sysUser/add";
	}

	@RequestMapping("/add")
	public String add(SysUser user,
	                  @RequestParam(required = false) MultipartFile idPic,
	                  HttpServletRequest request){
		logger.info("添加");

		String path = FileUtil.upload(request,idPic);
		logger.info("文件上传的路径："+path);

		//将创建的路径添加到用户中
		//user.setIdPicPath(path);
		//保存用户

		return "redirect:/user/list"; //重定向==》获取列表数据
	}



	@RequestMapping("/toUpdate")
	public String toUpdate(){
		logger.info("进入修改页面");
		return "sysUser/update";
	}

	@RequestMapping("/update")
	public String update(){
		logger.info("修改");
		return "redirect:user/list"; //重定向==》获取列表数据
	}

	@RequestMapping("/del")
	public String del(){
		logger.info("删除");
		return "sysUser/update";
	}

}
